package edu.chl.tda367.mynotes.ctrl;

import java.awt.Toolkit;
import java.io.File;

import javax.swing.JFileChooser;

import edu.chl.tda367.mynotes.core.MyNotes;
import edu.chl.tda367.mynotes.io.FileHandler;
import edu.chl.tda367.mynotes.io.IFileHandler;

/*
 * Takes care of what happens when a user press the saveAs document button.
 */

public class SaveDocAsCtrl implements ICtrl {

	private IFileHandler fileHandler = FileHandler.newInstance();
	private MyNotes mynotes = MyNotes.getInstance();
	private JFileChooser choose = new JFileChooser();

	/**
	 *  The user chooses where to save the document. The model is updated
	 *  after this choice.
	 */
	public void execute(){

		if(mynotes.getDocument() != null){
			if(choose.showSaveDialog(null) == JFileChooser.APPROVE_OPTION){
				File file = choose.getSelectedFile();

				fileHandler.saveFile(file, mynotes.getDocument());
				mynotes.setSavedText(mynotes.getText());
				mynotes.setFileName(choose.getSelectedFile().getName());
				mynotes.setFilePath(choose.getSelectedFile().getAbsolutePath());
				mynotes.setSavedDocument();
				mynotes.setIsSaved(true);
			}
		} else {
			Toolkit.getDefaultToolkit().beep();
		}
	}
	
	@Override
	public void execute(String s) {
		
	}
}
